namespace ShopForWeb.Dtos.ViewModel
{
  /// <summary>
  /// 分页栏Dto
  /// </summary>
  public class PageBarView
  {
    public int CurrentPage { get; set; }
    public int RecordCount { get; set; }

    public int Previous { get; set; }

    public int Next { get; set; }

    public int PageSize { get; set; }

    public int PageCount { get; }

    public string Url { get; set; } = String.Empty;

    public List<int> ButtonGroup { get; set; }

    public PageBarView(int currentPage, int pageSize, int recordCount)
    {
      this.CurrentPage = currentPage;
      this.PageSize = pageSize;
      this.RecordCount = recordCount;
      //计算页数
      this.PageCount = (recordCount + this.PageSize - 1) / this.PageSize;
      #region 分页按钮组,规则是当前页的前5个,后5个
      this.ButtonGroup = new List<int>();
      int start = 0, end = 0;
      if (this.CurrentPage - 5 <= 0) start = 1;
      else start = this.CurrentPage - 5;
      if (this.CurrentPage + 5 >= this.PageCount) end = this.PageCount;
      else end = this.CurrentPage + 5;
      for (int i = start; i <= end; i++) this.ButtonGroup.Add(i);
      #endregion
      //前一页
      if (this.CurrentPage - 1 <= 0) this.Previous = 1;
      else this.Previous = this.CurrentPage - 1;
      //后一页
      if (this.PageCount == 0) this.Next = 1;
      else if (this.CurrentPage + 1 >= this.PageCount) this.Next = this.PageCount;
      else this.Next = this.CurrentPage + 1;
    }
  }
}
